﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VMS.FTS.APPPUSH.Utils.FontData;

namespace VMS.FTS.APPPUSH.Utils
{
    public class StringUtils
    {
        static List<string> fontList = null;
        public static byte[] DecodeString(String content, int size = 7)
        {
            byte[] outBuf = new byte[content.Length];
            //if (fontList == null)
            {

                List<JsonCustomElement> Map = ElementsMap.Instance().Map;
                fontList = new List<string>();
                foreach (JsonCustomElement kPair in Map)
                {
                    if (kPair.Size == size)
                    {
                        fontList.Add(kPair.Name);
                    }
                }
            }

            for (int index = 0; index < content.Length; index++)
            {
                //if (size == 7)
                //    outBuf[index] = (byte)fontList.IndexOf(content[index].ToString());
                //else
                string data = content[index].ToString().ToUpper();
                outBuf[index] = (byte)fontList.IndexOf(data);
            }
            return outBuf;
        }

        public static string[] TokenizeString(String content)
        {
            if (content == null)
            {
                return null;
            }
            List<String> strings = new List<String>();
            /*
            foreach (HASH_TAG tag in EnumUtils.EnumToList<HASH_TAG>())
            {
                string tagString = EnumUtils.GetEnumDescription(tag);
                content.ind
            }
            */
            // Normalized string
            content.Replace("\n", "#n");

            // split into slices
            int i = 0;
            int lastIndex = 0;
            bool isFirst = false;
            while ((i = content.IndexOf('#', i)) != -1)
            {
                if (!isFirst) isFirst = true;
                // C.
                // Print out the substring.
                //Console.WriteLine(content.Substring(i, i - lastIndex));
                if (i > lastIndex)
                {
                    string temp = content.Substring(lastIndex, i - lastIndex);
                    if (temp[1] == 'n')
                    {
                        strings.Add(temp.Substring(0, 2));
                        if (temp.Length >= 2)
                            strings.Add(temp.Substring(2));
                    }
                    else
                    {
                        strings.Add(temp.Substring(0, 3));
                        if (temp.Length > 3)
                            strings.Add(temp.Substring(3));
                    }
                }
                // D.
                // Increment the index.
                lastIndex = i;
                i++;
            }
            if (lastIndex == 0 && !isFirst)
                strings.Add(content);
            else
            {
                string temp = content.Substring(lastIndex);
                if (temp[1] == 'n')
                {
                    strings.Add(temp.Substring(0, 2));
                    if (temp.Length >= 2)
                        strings.Add(temp.Substring(2));
                }
                else
                {
                    strings.Add(temp.Substring(0, 3));
                    if (temp.Length > 3)
                        strings.Add(temp.Substring(3));
                }
                //strings.Add(content.Substring(lastIndex));
            }
            return strings.ToArray();
        }
        enum HASH_TAG
        {
            [Description("#e0")]
            EFFECT_STATIC = 0x00,
            [Description("#e1")]
            EFFECT_TOPDOWN,
            [Description("#e2")]
            EFFECT_BOTTOMUP,
            [Description("#e3")]
            EFFECT_LEFTRIGHT,
            [Description("#e4")]
            EFFECT_RIGHTLEFT,

            [Description("#cr")]
            COLOR_RED,
            [Description("#cg")]
            COLOR_GREEN,
            [Description("#cy")]
            COLOR_YELLOW,

            [Description("#f0")]
            FONT_SMALL,
            [Description("#f1")]
            FONT_MEDIUM,
            [Description("#f2")]
            FONT_LARGE,

            [Description("#al")]
            ALIGN_LEFT,
            [Description("#ar")]
            ALIGN_RIGHT,
            [Description("#ac")]
            ALIGN_CENTER,



            [Description("#n")]
            NEWLINE,
            /*
             * Temporary NOT use
            [Description("#date")]
            CALENDAR_DATE,
            [Description("#time")]
            CALENDAR_TIME
             * */
        }
        public static byte[] ConvertText(String content)
        {
            // alloc buffer
            byte[] outBuf = new byte[content.Length];

            // Tokenize in put string
            string[] strings = TokenizeString(content);

            int offset = 0;
            int fontSize = 7;
            foreach (string s in strings)
            {
                if (s.Contains('#'))
                {

                    if (s.Equals("#f0"))
                    {
                        fontSize = 7;
                    }
                    else if (s.Equals("#f1"))
                    {
                        fontSize = 10;
                    }
                    if (s.Equals("#f2"))
                    {
                        fontSize = 12;
                    }

                    bool tagFound = false;
                    foreach (HASH_TAG tag in EnumUtils.EnumToList<HASH_TAG>())
                    {
                        string currentTag = EnumUtils.GetEnumDescription(tag);
                        //Console.Write("current tag: " + currentTag + "Input value: "+ s);
                        if (currentTag == s)
                        {
                            //  Console.Write(" ==> Matched!" + Environment.NewLine);
                            tagFound = true;
                            break;
                        }
                    }

                    if (tagFound)
                    {
                        (new System.Text.ASCIIEncoding()).GetBytes(s).CopyTo(outBuf, offset);
                        outBuf[offset] = 137;
                    }
                    else
                    {
                        DecodeString(s, fontSize).CopyTo(outBuf, offset);
                    }
                }
                else
                {
                    DecodeString(s, fontSize).CopyTo(outBuf, offset);
                }
                offset += s.Length;

            }
            return outBuf;
        }
    }
}
